define(function () {
    'use strict';
    function ctrl($scope, $state, $stateParams, $ionicLoading, LocalCacheService, WebService, TdxLocalFuncService) {
        function init() {
            $scope.user = LocalCacheService.getUser();
            if ($stateParams.go != 'must') {
                //判断用户风险等级
                if ($scope.user.corp_risk_status != 0 || $scope.user.corp_risk_level < 4) {
                    $state.go('tab.account-risk', {from: 'tab.account-OpenStocksHK', fromParams: $stateParams.typeid});
                    return;
                }
            }
            var params = {
                client_id: $scope.user.client_id,
                exchange_kind: $stateParams.typeid == 1 ? '10' : '20'
            };
            var title = $stateParams.typeid == 1 ? '沪A账户：' : '深A账户：';
            $scope.StocksHK = {
                title: $stateParams.typeid == 1 ? '沪港通' : '深港通',
                status: 0,//账户
                status_content: '',//状态说明
                isCheck: false//协议勾选
            };
            $scope.btn = {text: '立即开通'};//按钮
            $scope.openAccount = {content: '', isOpen: false};
            //查询港股通账户状态,查询前设置$ionicLoading,show();查询有返回就设置$ionicLoading,hide()
            //根据账户状态设置:$scope.StocksHK.status,$scope.StocksHK.status_content;
            //账户状态为未开通则再调用查询A股账户状态
            WebService.qryStockholderDetail(params).then(function (data) {
                if (data.open_status == '1') {//已开通
                    $scope.openAccount.content = title + data.stock_account;
                    $scope.openAccount.isOpen = true;
                } else if (data.open_status == '9') {
                    $scope.openAccount.content = title + '您已经提交了开户申请，系统正在处理中';
                    $scope.openAccount.isOpen = false;
                } else {
                    $scope.openAccount.content = title + '未开通';
                    $scope.openAccount.isOpen = false;
                }
            }, function () {
                $scope.openAccount.isOpen = false;
                $scope.openAccount.content = title + '获取信息错误';
            });
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init()
        });

        //检测条件
        $scope.check = function () {
            $scope.btn.text = '申请条件检测中，完成后自动跳转';
            $scope.openAccount.isOpen = false;//设置按钮状态
            //检测
            //检查是否签署过电子签名约定书提示
            //CommonService.showConfirm({btn1Text:'关闭', btn2Text:'立即签署', message:'您尚未签署电子签名约定书，需先签署电子签名约定书！'},function(){
            //
            // },function(){
            //     if (TdxLocalFuncService.isTDX()) {
            //         TdxLocalFuncService.openDzht().then(function (data) {
            //         });
            //     }
            // }):
            //检查提示并重置按钮状态
            //CommonService.showConfig({message:''});
            //重置按钮状态
            //$scope.openAccount.isOpen=true;
            //检测合格后
            $state.go('tab.account-OpenKnowledge', {typeid: $stateParams.typeid})
        }
    }

    ctrl.$inject = ['$scope', '$state', '$stateParams', '$ionicLoading', 'LocalCacheService', 'WebService', 'TdxLocalFuncService'];
    return ctrl;
});
